﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Brain
{
    public static class FloatUtil
    {
        /// <summary>
        /// Returns the Absolute value. Equivalente to Math.Abs(..)
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static float Absolute(float value)
        {
            return value < 0 ? -value : value;
        }

        /// <summary>
        /// Returns all the values on the floats / floats.count
        /// </summary>
        /// <param name="ints"></param>
        /// <returns></returns>
        public static float Average(List<float> floats)
        {
            float final = 0;
            for (int i = 0; i < floats.Count; i++)
                final += floats[i];
            final /= floats.Count;
            return final;
        }

        /// <summary>
        /// Clamps the value(Never goes minor than minValue, never goes greater than maxValue. 
        /// This is equivalent to the MathHelper.Clamp(...)
        /// </summary>
        /// <param name="value"></param>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <returns></returns>
        public static float Clamp(float value, float minValue, float maxValue)
        {
            if (value < minValue)
                value = minValue;
            if (value > maxValue)
                value = maxValue;

            return value;
        }

        /// <summary>
        /// Parses the float, with CultureInfo.InvariantCulture
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static float Parse(string str)
        {
            return float.Parse(str, CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Converts a float to string with the desired number
        /// of decimals
        /// </summary>
        /// <param name="decimalCases"></param>
        /// <returns></returns>
        public static string Format(float fl, int decimalCases)
        {
            string s = fl.ToString(CultureInfo.InvariantCulture);
            int point = s.IndexOf(",");
            if (point == -1)
                point = s.IndexOf(".");
            if (point == -1)
                return fl.ToString();

            if (s.Length > point + decimalCases + 1)
                s = s.Remove(point + decimalCases + 1);
            return s;
        }

        /// <summary>
        /// Converts a float to string with the desired number
        /// of decimals
        /// </summary>
        /// <param name="decimalHouses"></param>
        /// <returns></returns>
        public static string Format(float fl, int decimalHouses, bool scientificNotation)
        {
            float finalValue = fl;
            int tens = 0;

            while (finalValue > 10)
            {
                finalValue /= 10;
                tens++;
            }

            string s = finalValue.ToString();
            int point = s.IndexOf(",");
            if (point == -1)
                point = s.IndexOf(".");
            if (point == -1)
                return fl.ToString();

            if (s.Length > point + decimalHouses + 1)
                s = s.Remove(point + decimalHouses + 1);
            if (tens > 0)
            {
                return s + " 10 pow " + tens;
            }
            return s;
        }

        /// <summary>
        /// Linearly interpolates between a value to another
        /// </summary>
        /// <param name="value1"></param>
        /// <param name="value2"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public static float Lerp(float value1, float value2, float amount)
        {
            return value1 + (value2 - value1) * amount;
        }


    }
}
